package com.sq.partner.manager;

import java.util.Map;

import org.apache.log4j.Logger;

import com.sq.partner.manager.util.JMXUtils;


/**
 * 清空缓存服务
 * 
 * @author 
 *
 */
public class CacheManager {
	private static Logger logger = Logger.getLogger(CacheManager.class);
	/** 缓存对象名称 */
	private String name;
	/** JMX服务的serviceURL */
	private Map<String,String> serviceURL;
	/** JMX对象的objectName */
	private String objectName;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getObjectName() {
		return objectName;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}
	
	public Map<String, String> getServiceURL() {
		return serviceURL;
	}

	public void setServiceURL(Map<String, String> serviceURL) {
		this.serviceURL = serviceURL;
	}

	/**
	 * key 为服务器ip
	 * 清空缓存
	 */
	public void clearAllCache(String ip)  throws Exception{
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
			}
			try {
				JMXUtils.invoke(map.getValue(), objectName, "clearAllCache", new Object[0],new String[0]);
			} catch (Exception e) {
				logger.debug("链接异常:"+map.getValue());
			}
		}
	}
}
